package com.fengjue.controller;


import com.fengjue.common.RespBean;
import com.fengjue.pojo.Admin;
import com.fengjue.pojo.Role;
import com.fengjue.service.IAdminService;
import com.fengjue.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fengjue
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping
    public List<Admin> getAllAdmins(String keywords){
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "更新操作员")
    @PutMapping
    public RespBean updateAdmin(@RequestBody Admin admin){
        boolean b = adminService.updateById(admin);
        if (b){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/{id}")
    public RespBean updateAdmin(@PathVariable Integer id){
        boolean b = adminService.removeById(id);
        if (b){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }


    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        List<Role> list = roleService.list();
        return list;
    }


    @ApiOperation(value = "更新操作员的角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId,Integer[] rids ){

        RespBean respBean =    adminService.updateAdminRole(adminId,rids);
        return respBean;
    }



}
